package org.jsola.hr.task;

import lombok.extern.slf4j.Slf4j;
import org.jsola.common.DateKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.entity.CompanySysNoticeDO;
import org.jsola.hr.query.CompanySysNoticeQuery;
import org.jsola.hr.service.ICompanySysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统公告,个人通知超时定时任务
 *
 * @author zxy
 */
@Slf4j
@Component
public class NoticeTask {

    @Autowired
    private ICompanySysNoticeService companySysNoticeService;

    /**
     * 自动转正,将转正日期是今天的员工自动转为正式员工
     * 每天0点5分0秒触发
     */
    public void auditTurnStraightTask() {
        CompanySysNoticeQuery companySysNoticeQuery = new CompanySysNoticeQuery();
        List<CompanySysNoticeDO> noticeList = companySysNoticeService.selectDO(companySysNoticeQuery, "default");
        if (CollectionUtils.isEmpty(noticeList)) {
            return;
        }
        noticeList = noticeList.parallelStream().filter(
                notice -> {
                    int count = DateKit.getDaysBetween(notice.getPublishDate(), new Date());
                    return count >= 180;
                }
        ).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(noticeList)) {
            return;
        }
        for (CompanySysNoticeDO noticeListVO : noticeList) {
            companySysNoticeService.deleteByIds(noticeListVO.getSiteId(), noticeListVO.getId());

            HrLogKit.logForSave("超时定时任务", "auditTurnStraightTask", noticeListVO.getId(), null);
        }
    }


}
